package com.example.listvwork;
// 创建一个MainActivity类，继承自AppCompatActivity，并实现onCreate方法。
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    // 数据 goods drawable
    private  List<Goods> goodList = new ArrayList<>();
    // 初始化数据
    private void initData() {
        goodList.add(new Goods("桌子", R.drawable.qiang, "1800元"));
        goodList.add(new Goods("苹果", R.drawable.pinggou, "10元/kg"));
        goodList.add(new Goods("蛋糕", R.drawable.dangao, "300元"));
        goodList.add(new Goods("线衣", R.drawable.miaoyi, "350元"));
        goodList.add(new Goods("芒果", R.drawable.manggou, "？元"));
        goodList.add(new Goods("雨伞", R.drawable.yusan, "？元"));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 数据适配器
        initData();
        // listView
        GoodsADapter goodsADapter = new GoodsADapter(this, R.layout.goods_, goodList);
        ((ListView) findViewById(R.id.listView)).setAdapter(goodsADapter);
        // 为每一个item设置点击事件 弹出对应的图片 持续3s
        ((ListView) findViewById(R.id.listView)).setOnItemClickListener((parent, view, position, id) -> {
            Goods goods = goodList.get(position);
            int imageId = goods.getImageId();
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageId);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(imageView);
            // 设置图标大小占满窗口
            AlertDialog dialog =builder.create();
            dialog.show();
            new Thread(() -> {
                try {
                    Thread.sleep(2500);
                } catch (InterruptedException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
                runOnUiThread(dialog::dismiss);
            }).start();
                Toast.makeText(this,goods.getName(), Toast.LENGTH_SHORT).show();
        });

    }
}